const AdminBiz = require('../../../../store/admin_biz_config.js');
// 导入路径辅助模块，避免重复引用
const pathImport = require('../helpers/path_import.js');
const { pageHelper, cloudHelper, fileHelper, ProjectBiz, FlowBiz } = pathImport;;
const projectSetting = require('../../../../../../config/projects_config.js');
const { createStoreBindings } = require('mobx-miniprogram-bindings');
import { adminStore } from '../../../../config/store_config.js';

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		url: '',
		time: '',
		condition: '',

		formStart: '',
		formEnd: '',
		formCateId: '',
		formStatus: '999',

		isLoad: false,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		if (!AdminBiz.isAdmin(this)) return;

		if (options && options.condition) {
			this.setData({
				condition: options.condition
			})
		}

		let cateIdOptions = [];
		let steps = projectSetting.FLOW_ALL_STEPS;
		for (let k = 0; k < steps.length; k++) {
			cateIdOptions.push(steps[k].FLOW_CATE_ID + '=' + steps[k].FLOW_NAME);
		}
		this.setData({ cateIdOptions: cateIdOptions.join(',') });

		// 创建MobX绑定
		this.storeBindings = createStoreBindings(this, {
			store: adminStore,
			actions: ['getFlowData', 'exportFlowData', 'deleteFlowData']
		});

		this._loadDetail(1);
	},

	_loadDetail: async function (isDel) {
		if (!AdminBiz.isAdmin(this)) return;

		// 使用MobX Store的方法获取数据
		let data = await this.getFlowData(isDel);

		if (!data) return;

		this.setData({
			isLoad: true,
			url: data.url,
			time: data.time
		})
	},


	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {
		// 销毁MobX绑定
		this.storeBindings && this.storeBindings.destroyStoreBindings();
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: async function () {
		await this._loadDetail(1);
		wx.stopPullDownRefresh();
	},

	bindOpenTap: function (e) {
		fileHelper.openDoc('客户数据', this.data.url);
	},

	url: async function (e) {
		pageHelper.url(e, this);
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	bindExportTap: async function (e) {
		let start = this.data.formStart;
		if (!start) return pageHelper.showModal('请选择起始时间');

		let end = this.data.formEnd;
		if (!end) return pageHelper.showModal('请选择结束时间');

		let cateId = this.data.formCateId;
		if (!cateId) return pageHelper.showModal('请选择分类');

		let steps = projectSetting.FLOW_ALL_STEPS;
		let fields = [];
		for (let k = 0; k < steps.length; k++) {
			if (steps[k].FLOW_CATE_ID == cateId) {
				fields = steps[k].FLOW_FIELDS;
				break;
			}
		}

		try {
			let params = {
				condition: this.data.condition,
				start,
				end,
				status: this.data.formStatus,
				cateId,
				fields
			}

			// 使用MobX Store的方法导出数据
			let res = await this.exportFlowData(params);

			this._loadDetail(0);
			pageHelper.showModal('数据文件生成成功(' + res.total + '条记录), 请点击「直接打开」按钮或者复制文件地址下载');
		} catch (err) {
			console.log(err);
			pageHelper.showNoneToast('导出失败，请重试');
		}
	},

	bindDelTap: async function (e) {
		try {
			// 使用MobX Store的方法删除数据
			await this.deleteFlowData();
			
			this.setData({
				url: '',
				time: ''
			});
			pageHelper.showSuccToast('删除成功');
		} catch (err) {
			console.log(err);
			pageHelper.showNoneToast('删除失败，请重试');
		}
	},
})